package cn.wolfcode.luowowo.website.util;

import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.alibaba.fastjson.JSON;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {

    public static void addCookie(String key, String token, int time, HttpServletResponse response){
        if(token != null){
            Cookie c = new Cookie(key, token);
            c.setMaxAge(time);
            c.setPath("/");
            response.addCookie(c);
        }
    }

    public static String getCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if(cookies != null){ // 注意记得判空
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("token")) {
                    String token = cookie.getValue();
                    return token;
                }
            }
        }
        return null;
    }

}
